package com.jic.order.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

/**
 * 
 * 报文加解密
 *
 */
@SuppressWarnings("restriction")
public class DatagramEncDec {


   public static String sKey ="@#$%^&UuIj5@1234";
   public static String sIv ="11qWh^^&*(*94422";

   private static final Logger LOGGER = LoggerFactory.getLogger(DatagramEncDec.class);

	private static Charset CHARSET = Charset.forName("UTF-8");

    public static String encrypt(String datagram,String sKey,String sIv) throws Exception {
        LOGGER.info("原报文:"+datagram);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE,new SecretKeySpec(sKey.getBytes(),"AES"),
                new IvParameterSpec(sIv.getBytes()));
        byte[] encrypted = cipher.doFinal(datagram.getBytes());
        String encrypt = new String(Base64.encodeBase64(encrypted));
        LOGGER.info("加密报文:"+encrypt);
        return  encrypt;
    }

    public static String decrypt(String datagram,String sKey,String sIv) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        Cipher.getInstance("DES/ECB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(sKey.getBytes(),"AES"),new IvParameterSpec(sIv.getBytes()));
        byte[] baseRes = Base64.decodeBase64(datagram.getBytes());
        return new String(cipher.doFinal(baseRes));
    }


    public static String wxDecrypt(String encryptedData,String sessionKey,String vi) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      //  Cipher.getInstance("DES/ECB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec((Base64.decodeBase64(sessionKey)),"AES"),new IvParameterSpec((Base64.decodeBase64(vi))));
        byte[] baseRes = Base64.decodeBase64(encryptedData.getBytes());
        return new String(cipher.doFinal(baseRes));
    }

    public static String sign(String content,  String charset){
        try {
            return DigestUtils.md5Hex(content.getBytes(charset)).toUpperCase();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static boolean verify(String encrypt,String decrypt) {
        if(null==encrypt || null==decrypt
                || !encrypt.equals(decrypt)){
            return false;
        }
        return true;
    }

    public static void main(String[] args) throws Exception {
        JSONObject json = new JSONObject();
        json.put("awardId","1");
//        json.put("memberId","1");
//        json.put("openId","666999");
//        json.put("goodsIds","1-2");
//        json.put("price","840");
//        json.put("usedCommission","30");
        String de =  encrypt(json.toJSONString(),DatagramEncDec.sKey,DatagramEncDec.sIv);
        System.out.println("密文:"+de);
        String s = "oeio1by2xcyMubnO7R/1rg==";
        System.out.println(decrypt(de,sKey,sIv));
    }

//    public static boolean isAdmin(String openId){
//        if(StringUtils.isEmpty(openId)){
//            return false;
//        }
//        for(String adminOpenId:adminOpenId){
//            if(adminOpenId.equals(openId)){
//                return true;
//            }
//        }
//        return false;
//    }

}
